//
//  AlertDeviceDetailViewViewController.m
//  CityFire
//
//  Created by  androidlongs on 2017/11/2.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "AlertDeviceDetailViewViewController.h"
#import "UITextView+Placeholder.h"
#import "UserInfoModel.h"
#import "YQNetworking+RequestManager.h"

@interface AlertDeviceDetailViewViewController ()
@property(strong,nonatomic)UITextView *intputTextView;
@end

@implementation AlertDeviceDetailViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"修改设备信息";
    self.view.backgroundColor=[UIColor colorWithHexString:@"#efefef"];
    
    [self initView];
    
    [self initDatas];
    
}

- (void)initView {
    
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.frame = CGRectMake(10, 20, mainW-20, 40);
    [titleLabel setFont:[UIFont systemFontOfSize:16]];
    [titleLabel setBackgroundColor:[UIColor whiteColor]];
    [titleLabel setTextColor:[UIColor colorWithHexString:@"#4a4a4a"]];
    titleLabel.text=self.alertTitleKey;
    [self.view addSubview:titleLabel];
    
    UITextView *intputTextView = [[UITextView alloc]init];
    intputTextView.frame = CGRectMake(10, 70, mainW-20, 120);
    [intputTextView setTextColor:[UIColor grayColor]];
    intputTextView.text = self.alertTitleValue;
    [intputTextView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:intputTextView];
    
    self.intputTextView = intputTextView;
    
    
    UIButton *commitButton = [[UIButton alloc]init];
    commitButton.frame = CGRectMake(mainW-150, 210, 140, 40);
    [commitButton setTintColor:[UIColor whiteColor]];
    commitButton.titleLabel.textAlignment=NSTextAlignmentCenter;
    [commitButton addTarget:self action:@selector(commitAlertInformation) forControlEvents:UIControlEventTouchUpInside];
    
    [commitButton setTitle:@"保存" forState:UIControlStateNormal];
    [commitButton setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:commitButton];
    
}


-(void)initDatas{
    if (self.sesion==0) {
        switch (self.raw) {
            case 0:{
                //设备类型
                self.intputTextView.text=self.model.deviceType1;
            }
                break;
            case 2:{
                //设备名称
                self.intputTextView.text=self.model.deviceName1;
            }
                break;
            case 3:{
                //设备所在单位
                self.intputTextView.text=self.model.deviceOrg;
            }
                break;
            case 4:{
                //设备所在地址
                self.intputTextView.text=self.model.deviceAddress1;
            }
                break;
            default:
                break;
        }
    } else if (self.sesion==1){
        switch (self.raw) {
            case 0:{
                //经度
                self.intputTextView.text=self.model.latitude;
            }
                break;
            case 1:{
                //纬度
                self.intputTextView.text=self.model.longitude;
            }
                break;
                
            default:
                break;
        }
    } else if (self.sesion==2){
        switch (self.raw) {
            case 0:{
                //探测器数量
                self.intputTextView.text=[NSString stringWithFormat:@"%ld", self.model.detectorAmount1];
            }
                break;
            case 1:{
                //控制器型号
                self.intputTextView.text=self.model.controllerSpec1;
            }
                break;
            case 2:{
                //控制器厂家
                self.intputTextView.text=self.model.controllerManufacturer1;
            }
                break;
            case 3:{
                //产品编号
                self.intputTextView.text=self.model.itemCode1;
            }
                break;
            case 4:{
                //采购时间
                self.intputTextView.text=self.model.purchaseTime1;
            }
                break;
            default:
                break;
        }
    }else if (self.sesion==3){
        switch (self.raw) {
            case 0:{
                //售后电话
                self.intputTextView.text=self.model.hotline;
            }
                break;
            case 1:{
                //维护联系人
                self.intputTextView.text=self.model.linkMan1;
            }
                break;
            case 2:{
                //维保年限
                self.intputTextView.text=self.model.wbnx;
            }
                break;
            case 3:{
                //维护联系人电话
                self.intputTextView.text=self.model.phone;
            }
                break;
                
            default:
                break;
        }
    }else if (self.sesion==4){
        switch (self.raw) {
            case 0:{
                //备注
                self.intputTextView.text=self.model.remark;
            }
                break;
            default:
                break;
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)commitAlertInformation{
    
    NSString *alertValue =  self.intputTextView.text;
    
    if (!alertValue.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入修改内容"];
        return;
    }
    
    if (self.sesion==0) {
        switch (self.raw) {
            case 0:{
                //设备类型
                self.model.deviceType1=self.intputTextView.text;
            }
                break;
            case 2:{
                //设备名称
                self.model.deviceName1=alertValue;
            }
                break;
            case 3:{
                //设备所在单位
               self.model.deviceOrg=alertValue;
            }
                break;
            case 4:{
                //设备所在地址
                self.model.deviceAddress1=alertValue;
            }
                break;
            default:
                break;
        }
    } else if (self.sesion==1){
        switch (self.raw) {
            case 0:{
                //经度
                self.model.latitude=alertValue;
            }
                break;
            case 1:{
                //纬度
                self.model.longitude=alertValue;
            }
                break;
                
            default:
                break;
        }
    } else if (self.sesion==2){
        switch (self.raw) {
            case 0:{
                //探测器数量
               self.model.detectorAmount1 =[alertValue intValue];
            }
                break;
            case 1:{
                //控制器型号
               self.model.controllerSpec1=alertValue;
            }
                break;
            case 2:{
                //控制器厂家
                self.model.controllerManufacturer1=alertValue;
            }
                break;
            case 3:{
                //产品编号
                self.model.itemCode1=alertValue;
            }
                break;
            case 4:{
                //采购时间
                self.model.purchaseTime1=alertValue;
            }
                break;
            default:
                break;
        }
    }else if (self.sesion==3){
        switch (self.raw) {
            case 0:{
                //售后电话
               self.model.hotline=alertValue;
            }
                break;
            case 1:{
                //维护联系人
               self.model.linkMan1=alertValue;
            }
                break;
            case 2:{
                //维保年限
                self.model.wbnx=alertValue;
            }
                break;
            case 3:{
                //维护联系人电话
                self.model.phone=alertValue;
            }
                break;
                
            default:
                break;
        }
    }else if (self.sesion==4){
        switch (self.raw) {
            case 0:{
                //备注
               self.model.remark=alertValue;
            }
                break;
            default:
                break;
        }
    }
    
    
    NSMutableDictionary * paras =[[NSMutableDictionary alloc]init];
    
    [paras setValue:self.model.deviceType1 forKey:@"deviceType"];
    [paras setValue:[NSString stringWithFormat:@"%ld",self.model.deviceCode1] forKey:@"deviceCode"];
    [paras setValue:self.model.deviceName1 forKey:@"deviceName"];
    [paras setValue:self.model.deviceOrg forKey:@"deviceOrg"];
    [paras setValue:self.model.deviceAddress1 forKey:@"deviceAddress"];
    
    [paras setValue:self.model.longitude forKey:@"longitude"];
    [paras setValue:self.model.latitude  forKey:@"latitude"];
    
    [paras setValue:[NSString stringWithFormat:@"%ld",self.model.detectorAmount1] forKey:@"detectorAmount"];
    [paras setValue: self.model.controllerSpec1 forKey:@"controllerSpec"];
    [paras setValue:self.model.controllerManufacturer1 forKey:@"controllerManufacturer"];
    [paras setValue: self.model.itemCode1 forKey:@"itemCode"];
    [paras setValue:self.model.purchaseTime1 forKey:@"purchaseTime"];
    
    [paras setValue: self.model.hotline forKey:@"hotline"];
    [paras setValue:self.model.linkMan1 forKey:@"linkMan"];
    [paras setValue:self.model.phone forKey:@"phone"];
    [paras setValue:self.model.wbnx forKey:@"wbnx"];
    
    
    NSLog(@"self.model.status %@",self.model.status1);
     [paras setValue:self.model.status1 forKey:@"status"];
    
    [paras setValue:[NSString stringWithFormat:@"%ld",self.model.num] forKey:@"num"];
    
    
    
    
    [paras setValue:self.model.remark forKey:@"remark"];
    
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        
         [paras setValue:model.seeeionID forKey:@"sessionid"];
        [paras setValue:model.userName forKey:@"username"];
        
        [YQNetworking requestWithUrl:kDeviceUpdate_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:paras progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            
            if (responseInfo.code == 0) {
                 self.callBackBlock(self.model);
                [SVProgressHUD showSuccessWithStatus:@"提交成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
               [SVProgressHUD showErrorWithStatus:@"服务异常"];
            }
            
        } failBlock:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"服务异常"];
        }];
    }
    
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
